package com.fs.domain.helper;

import com.fs.bean.MonitorFalconTrend;
import com.fs.constant.DbInfoEnum;
import com.fs.vo.MaxQpsTrendVo;
import org.apache.commons.collections4.CollectionUtils;

import java.util.*;

/**
 * Created by fengshuai on 17/6/16.
 */
public class MonitorFalconTrendHelper {

    public static MaxQpsTrendVo transToVo(List<MonitorFalconTrend> trends) {
        if (CollectionUtils.isEmpty(trends)) {
            return new MaxQpsTrendVo();
        }
        MaxQpsTrendVo vo = new MaxQpsTrendVo();
        List<Map<String, Object>> series = new ArrayList<>();
        Map<Integer, Map<String, Object>> db2VoMap = new HashMap<>();
        Collections.sort(trends, new Comparator<MonitorFalconTrend>() {
            @Override
            public int compare(MonitorFalconTrend o1, MonitorFalconTrend o2) {
                return Integer.compare(o1.getDay(), o2.getDay());
            }
        });
        for (MonitorFalconTrend trend : trends) {
            Map<String, Object> map = db2VoMap.get(trend.getDb_id());
            if (map == null) {
                map = new HashMap<>();
                db2VoMap.put(trend.getDb_id(), map);
            }
            map.put("name", DbInfoEnum.getNameById(trend.getDb_id()));
            List<Integer> list = (List<Integer>) map.get("data");
            if (list == null) {
                list = new ArrayList<>();
                map.put("data", list);
            }
            list.add((int)trend.getQps());
        }
        for (Integer key : db2VoMap.keySet()) {
            series.add(db2VoMap.get(key));
        }
        vo.setSeries(series);
        return vo;
    }
}
